package com.xyzwps.mvckotlin.service

import com.xyzwps.mvckotlin.dao.repository.InvitationCodeRepository
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class InvitationCodeService(val invitationCodeRepository: InvitationCodeRepository) {

    companion object {
        private val log = LoggerFactory.getLogger(InvitationCodeService::class.java)
    }

    suspend fun checkCodeExists(code: String): Boolean {
        log.debug("try to get invitation code record of $code")
        val invitationCode = invitationCodeRepository.getUnusedByCode(code).await()
        return invitationCode != null
    }

    suspend fun markAsUsed(code: String): Unit = invitationCodeRepository.markAsUsed(code)

    suspend fun hasMoreUnusedInvitationCodes(): Boolean {
        val count = invitationCodeRepository.countUnusedInvitationCode().await()
        return count > 0
    }
}